package com.example.springbootshiro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootshiro.dao.OfficeContentMapper;
import com.example.springbootshiro.entity.OfficeContent;
import com.example.springbootshiro.service.OfficeContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OfficeContentServiceImpl implements OfficeContentService {

    @Autowired
    private OfficeContentMapper officeContentMapper;


    @Transactional
    @Override
    public List<OfficeContent> add(List<OfficeContent> param) {
        for (OfficeContent content :param){
            officeContentMapper.insert(content);
        }
        return param;
    }

    @Override
    public List<OfficeContent> selectAll(String param,String param2, String param3) {
        QueryWrapper<OfficeContent> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(param) && !"".equals(param)){
            queryWrapper.and(QueryWrapper -> QueryWrapper.like("content", param).or().like("file_name", param).or().like("remark", param));
            //queryWrapper.like("content", param).or().like("file_name",param).or().like("remark", param);
        }
        if (StringUtils.isNotBlank(param2) && !"".equals(param2)){
            queryWrapper.and(QueryWrapper-> QueryWrapper.gt("price",param2));
        }
        if (StringUtils.isNotBlank(param3) && !"".equals(param3)){
            queryWrapper.and(QueryWrapper-> QueryWrapper.lt("price",param3));
        }

        queryWrapper.orderByDesc("id");
        return officeContentMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<OfficeContent> pageOfficeContent(int page, int size, String param) {
        Page<OfficeContent> page1 = new Page<>(page, size);
        //LambdaQueryWrapper<OfficeContent> lambdaWrapper = new LambdaQueryWrapper<>();
        //lambdaWrapper.like(OfficeContent::getContent, param);
        QueryWrapper<OfficeContent> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(param) && !"".equals(param)){
            queryWrapper.like("content", param);
        }
        IPage<OfficeContent> iPage = officeContentMapper.selectPage(page1,queryWrapper);
        return iPage;
    }

    @Override
    public List<OfficeContent> selectByIds(List<Long> ids) {
        QueryWrapper<OfficeContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        return officeContentMapper.selectList(queryWrapper);
    }

    @Override
    public OfficeContent selectById(Long id) {
        return officeContentMapper.selectById(id);
    }
}
